const router = require('koa-router')()
var mongoose = require('mongoose');
var act = require('../controller/common.js');
var Per = mongoose.model('PerManage');
router.prefix('/p')
//获取单个的项目信息 ok
router.get('/:id', async (ctx, next) =>{
  let _id = ctx.params.id;
  let data = await act.getProjectById(_id);  
  ctx.body = data;
})
//TODO: 更新项目 
router.post('/up/:id', async (ctx, next) =>{
  let _id = ctx.params.id;
  let req = ctx.request;
  let name = req.body.name;
  let type = req.body.type;
  let memberId = [];
  memberId.push({id:_id});
  let pro = new Per({
    name :name,
    userId : _id,
    perManageDesc:'',
    memberId:memberId,
    type:type
  })
  let data = await act.createProjectByName(pro);
  ctx.body = data;
})
//创建项目 ok
router.post('/:id', async (ctx, next) =>{
  let _id = ctx.params.id;
  let req = ctx.request;
  let name = req.body.name;
  let type = req.body.type;
  let simpleDesc = req.body.simpleDesc;

  let memberId = [];
  memberId.push({id:_id});
  let pro = new Per({
    name :name,
    userId : _id,
    simpleDesc:simpleDesc,
    perManageDesc:'',
    memberId:memberId,
    type:type
  })
  let data = await act.createProjectByName(pro);
  ctx.body = data;
})
//查找用户的所有项目 ok
router.get('/u/:id', async (ctx, next) =>{
  let _id = ctx.params.id;
  let data = await act.getProjectByUserId(_id);
  ctx.body = data;
})

module.exports = router